package com.jomoo.common.tools;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginFilter extends HttpServlet implements Filter {

	private static final long serialVersionUID = -7668416001732622366L;

	public void doFilter(ServletRequest sRequest, ServletResponse sResponse,
			FilterChain filterChain) throws IOException, ServletException 
	{
		HttpServletRequest request = (HttpServletRequest) sRequest;
		HttpServletResponse response = (HttpServletResponse) sResponse;

		HttpSession session = request.getSession();
		String url = request.getServletPath();
		//String contextPath = request.getContextPath();
		
		
		
		if (null == session.getAttribute("user") || ("".equals(session.getAttribute("user")))) 
		{
			if (url.startsWith("index")
				|| url.startsWith("head")
				|| url.startsWith("left")
				|| url.startsWith("printTrashBatchSignTop")
				|| url.startsWith("postSecondTop")
				|| url.startsWith("postSecondList")
				|| url.startsWith("policyRefuseSign")
				|| url.startsWith("policyMailSign")
				|| url.startsWith("policyMailQuery")
				|| url.startsWith("policyConfirmSign")
				|| url.startsWith("/PackDept/")
				|| url.startsWith("/PostSnd/")
				|| url.startsWith("/Sign/")) 
			{
				RequestDispatcher dispatcher = request.getRequestDispatcher("/LoginError.jsp");
				dispatcher.forward(request, response);
				
				return;
			}
			
			String organId = request.getParameter("organ_id");

			String deptId = request.getParameter("dept_id");

			String deptName = request.getParameter("dept_name");

			String userName = (String) request.getParameter("user_name");

			String password = (String) request.getParameter("user_password");
			if (null == organId || null == deptId || null == deptName
					|| null == userName || null == password || "".equals(organId)
					|| "".equals(deptId) || "".equals(deptName)
					|| "".equals(userName) || "".equals(password)) 
			{
				request.removeAttribute("organ_id");
				request.removeAttribute("dept_id");
				request.removeAttribute("dept_name");
				request.removeAttribute("user_name");
				request.removeAttribute("user_password");
				RequestDispatcher dispatcher = request
						.getRequestDispatcher("/LoginError.jsp");
				dispatcher.forward(request, response);
				return;
			} 
			else 
			{
				//response.sendRedirect(contextPath + "/index.jsp");
				filterChain.doFilter(request, response);
			}
			
		}
		else
		{
			filterChain.doFilter(request, response);
		}
		


	}

	public void init(FilterConfig arg0) throws ServletException {

	}
}
